﻿
$(document).ready(function () {
    $(".calendar-date").datepicker();
});

// First: Create config
ko.validation.configure({
	registerExtenders: true,
	messagesOnModified: true,
	insertMessages: true,
	parseInputAttributes: true,
	messageTemplate: null,
	decorateElement: true,
	errorClass: "error",
	errorMessageClass: "error-desc"
});

// Second: Create new model to apply to view
var viewModel = new function () {
    // About you
    this.Title = ko.observable().extend({ maxLength: 250 }).extend({ required: { message: "Tiêu đề tài liệu là bắt buộc"} });
    this.ShortDesc = ko.observable();
    this.LongDesc = ko.observable();
    this.ImageUrl = ko.observable();
    this.FileName = ko.observable();
    this.Active = ko.observable(true);
    this.IsTop = ko.observable(false);
    this.IsSpecial = ko.observable(false);
    this.AcceptPayment = ko.observable(false);

    // Categories
    this.AllCategories = ko.observable(false);
    this.CategoryList = ko.observableArray(data.ListCategory);
    this.CategoryID = ko.observable(data.ListCategory[1].ID);
    this.SubCategoryOptionList = ko.observableArray(data.ListSubCategory);
    this.SubCategoryList = ko.computed(function () {
        var cate = this.CategoryID();
        return ko.utils.arrayFilter(this.SubCategoryOptionList(), function (option) {
            return option.ParentID == cate;
        });
        var tempEl = document.getElementById("SubCategoryID");
        if (typeof tempEl.jcf != "undefined" && tempEl.jcf) tempEl.jcf.destroy();
        jcf.customForms.replaceElement("SubCategoryID");
        setTimeout("jcf.customForms.replaceElement('SubCategoryID')", 0);
    }, this);
    this.SubSelectedID = ko.observable(this.SubCategoryList()[0].ID);

    // State
    this.AllStates = ko.observable(false);
    this.StateList = ko.observableArray(data.ListState);
    this.StateID = ko.observable(data.ListState[1].ID);
    this.DistrictOptionList = ko.observableArray(data.ListDistrict);
    this.DistrictList = ko.computed(function () {
        var state = this.StateID();
        return ko.utils.arrayFilter(this.DistrictOptionList(), function (option) {
            return option.ParentID == state;
        });
    }, this);
    this.DistrictSelectedID = ko.observable(this.DistrictList()[0].ID);

    // Option
    this.OptionList = ko.observableArray(data.OptionList);
    this.OptionID = ko.observable(data.OptionList[0].ID);

    // Group
    this.GroupList = ko.observableArray(data.GroupList);
    this.GroupID = ko.observable(data.GroupList[0].ID);

    // Payment
    this.PaymentList = ko.observableArray(data.PaymentList);
    this.PaymentID = ko.observable(data.PaymentList[0].ID);


    // Submit, Next button
    this.InvalidateForm = ko.observable(false);
    this.SubmitForm = function () {
        if (viewModel.errors().length == 0) {
            $('form').submit();
        }
        else {
            viewModel.errors.showAllMessages();
            this.InvalidateForm(true);
        }
    }
};

viewModel.errors = ko.validation.group(viewModel);

// Third: Apply view model to view to handle action when user input data
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(viewModel, document.getElementById("main"));